#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Token 类型
typedef enum
{
    TOKEN_TYPE,
    TOKEN_IDENTIFIER,
    TOKEN_SEMICOLON,
    TOKEN_COMMA,
    TOKEN_ASSIGN,
    TOKEN_NUMBER
} TokenType;

// Token 结构体
typedef struct Token
{
    TokenType type;
    char *value;
    struct Token *next;
} Token;

// 抽象语法树节点类型
typedef enum
{
    AST_VARIABLE_DECLARATION,
    AST_VARIABLE_INITIALIZATION
} ASTNodeType;

// 抽象语法树节点结构体
typedef struct ASTNode
{
    ASTNodeType type;
    Token *type_token;
    Token *identifier_token;
    Token *initial_value_token;
    struct ASTNode *next;
} ASTNode;

// 生成新的 Token
Token *new_token(TokenType type, const char *value)
{
    Token *token = (Token *)malloc(sizeof(Token));
    token->type = type;
    token->value = strdup(value);
    return token;
}

// 生成新的抽象语法树节点
ASTNode *new_ast_node(ASTNodeType type, Token *type_token, Token *identifier_token, Token *initial_value_token)
{
    ASTNode *node = (ASTNode *)malloc(sizeof(ASTNode));
    node->type = type;
    node->type_token = type_token;
    node->identifier_token = identifier_token;
    node->initial_value_token = initial_value_token;
    node->next = NULL;
    return node;
}

// 解析输入字符串为 Token 数组
Token **tokenize(const char *input, int *token_count)
{
    Token **tokens = NULL;
    *token_count = 0;
    int len = strlen(input);
    int i = 0;
    while (i < len)
    {
        // 跳过空白字符
        if (input[i] == ' ' || input[i] == '\t' || input[i] == '\n')
        {
            i++;
            continue;
        }
        // 处理关键字或标识符
        if ((input[i] >= 'a' && input[i] <= 'z') || (input[i] >= 'A' && input[i] <= 'Z') || input[i] == '_')
        {
            int start = i;
            while ((input[i] >= 'a' && input[i] <= 'z') || (input[i] >= 'A' && input[i] <= 'Z') || (input[i] >= '0' && input[i] <= '9') || input[i] == '_')
            {
                i++;
            }
            int token_len = i - start;
            char *token_str = (char *)malloc((token_len + 1) * sizeof(char));
            strncpy(token_str, input + start, token_len);
            token_str[token_len] = '\0';
            tokens = (Token **)realloc(tokens, (*token_count + 1) * sizeof(Token *));
            if (*token_count == 0)
            {
                tokens[*token_count] = new_token(TOKEN_TYPE, token_str);
            }
            else
            {
                tokens[*token_count] = new_token(TOKEN_IDENTIFIER, token_str);
            }
            (*token_count)++;
            free(token_str);
        }
        // 处理分号
        else if (input[i] == ';')
        {
            tokens = (Token **)realloc(tokens, (*token_count + 1) * sizeof(Token *));
            tokens[*token_count] = new_token(TOKEN_SEMICOLON, ";");
            (*token_count)++;
            i++;
        }
        // 处理逗号
        else if (input[i] == ',')
        {
            tokens = (Token **)realloc(tokens, (*token_count + 1) * sizeof(Token *));
            tokens[*token_count] = new_token(TOKEN_COMMA, ",");
            (*token_count)++;
            i++;
        }
        // 处理赋值符号
        else if (input[i] == '=')
        {
            tokens = (Token **)realloc(tokens, (*token_count + 1) * sizeof(Token *));
            tokens[*token_count] = new_token(TOKEN_ASSIGN, "=");
            (*token_count)++;
            i++;
        }
        // 处理数字
        else if (input[i] >= '0' && input[i] <= '9')
        {
            int start = i;
            while (input[i] >= '0' && input[i] <= '9')
            {
                i++;
            }
            int token_len = i - start;
            char *token_str = (char *)malloc((token_len + 1) * sizeof(char));
            strncpy(token_str, input + start, token_len);
            token_str[token_len] = '\0';
            tokens = (Token **)realloc(tokens, (*token_count + 1) * sizeof(Token *));
            tokens[*token_count] = new_token(TOKEN_NUMBER, token_str);
            (*token_count)++;
            free(token_str);
        }
        // 处理其他字符
        else
        {
            fprintf(stderr, "Error: Unexpected character '%c' at position %d\n", input[i], i);
            i++;
        }
    }
    return tokens;
}

// 根据 Token 数组生成抽象语法树
ASTNode *parse(Token **tokens, int token_count)
{
    if (token_count < 3)
    {
        return NULL;
    }
    ASTNode *head = NULL;
    ASTNode *tail = NULL;
    int i = 1;
    while (i < token_count - 1)
    {
        Token *identifier_token = tokens[i];
        Token *initial_value_token = NULL;
        if (i + 1 < token_count - 1 && tokens[i + 1]->type == TOKEN_ASSIGN)
        {
            initial_value_token = tokens[i + 2];
            i += 2;
        }
        ASTNode *node = new_ast_node(AST_VARIABLE_INITIALIZATION, tokens[0], identifier_token, initial_value_token);
        if (head == NULL)
        {
            head = node;
            tail = node;
        }
        else
        {
            tail->next = node;
            tail = node;
        }
        i++;
    }
    return head;
}

// 释放 Token 数组内存
void free_tokens(Token **tokens, int token_count)
{
    for (int i = 0; i < token_count; i++)
    {
        free(tokens[i]->value);
        free(tokens[i]);
    }
    free(tokens);
}

// 释放抽象语法树内存
void free_ast(ASTNode *node)
{
    while (node != NULL)
    {
        ASTNode *next = node->next;
        free(node->type_token->value);
        free(node->type_token);
        free(node->identifier_token->value);
        free(node->identifier_token);
        if (node->initial_value_token != NULL)
        {
            free(node->initial_value_token->value);
            free(node->initial_value_token);
        }
        free(node);
        node = next;
    }
}

// 打印抽象语法树信息
void print_ast(ASTNode *ast)
{
    ASTNode *current = ast;
    while (current != NULL)
    {
        printf("Type: %s\n", current->type_token->value);
        printf("Identifier: %s\n", current->identifier_token->value);
        if (current->initial_value_token != NULL)
        {
            printf("Initial Value: %s\n", current->initial_value_token->value);
        }
        else
        {
            printf("Initial Value: None\n");
        }
        printf("\n");
        current = current->next;
    }
}

// 打印所有 Token 信息
void print_tokens(Token **tokens, int token_count)
{
    for (int i = 0; i < token_count; i++)
    {
        switch (tokens[i]->type)
        {
        case TOKEN_TYPE:
            printf("Token Type: TOKEN_TYPE, Value: %s\n", tokens[i]->value);
            break;
        case TOKEN_IDENTIFIER:
            printf("Token Type: TOKEN_IDENTIFIER, Value: %s\n", tokens[i]->value);
            break;
        case TOKEN_SEMICOLON:
            printf("Token Type: TOKEN_SEMICOLON, Value: %s\n", tokens[i]->value);
            break;
        case TOKEN_COMMA:
            printf("Token Type: TOKEN_COMMA, Value: %s\n", tokens[i]->value);
            break;
        case TOKEN_ASSIGN:
            printf("Token Type: TOKEN_ASSIGN, Value: %s\n", tokens[i]->value);
            break;
        case TOKEN_NUMBER:
            printf("Token Type: TOKEN_NUMBER, Value: %s\n", tokens[i]->value);
            break;
        default:
            printf("Token Type: UNKNOWN, Value: %s\n", tokens[i]->value);
            break;
        }
    }
}

// // 变量声明语句
// declaration_statement ::= type_specifier variable_declaration_list ';'

// // 类型说明符
// type_specifier ::= 'int' | 'char' | 'float' | 'double' | ...  // 可根据需求扩展更多类型

// // 变量声明列表
// variable_declaration_list ::= variable_declaration (',' variable_declaration)*

// // 变量声明
// variable_declaration ::= identifier ('=' constant_expression)?

// // 标识符
// identifier ::= letter (letter | digit | '_')*

// // 常量表达式
// constant_expression ::= digit+

// // 字母
// letter ::= 'a' | 'b' | ... | 'z' | 'A' | 'B' | ... | 'Z'

// // 数字
// digit ::= '0' | '1' | ... | '9'

void variable_declaration(Token *tk);
void type_specifier(Token *tk);
void declaration_statement(Token *tk);
void variable_declaration_list(Token *tk);
void identifier(Token *tk);
void constant_expression(Token *tk);
void letter(Token *tk);
void digit(Token *tk);

void variable_declaration(Token *tk)
{
}

void identifier(Token *tk)
{
}

void constant_expression(Token *tk)
{
}

void letter(Token *tk)
{
}

void digit(Token *tk)
{
}

void variable_declaration_list (Token *tk)
{
}

void type_specifier(Token *tk)
{
}

void declaration_statement(Token *tk)
{
    type_specifier(tk);
    tk=tk->next;

    variable_declaration(tk);
    if(tk->type != TOKEN_SEMICOLON) {
        printf("error\n");
    }
}

void program(Token *tk)
{
    if (tk->type != TOKEN_TYPE)
    {
        printf("is invalid sprogram\n");
        return;
    }
    else
    {
        // tk = tk
    }
}

int main()
{
    int a =10 , b=0;
    const char *inputs = "int a,int b = 0,c,d=11,e;";

    Token *head = nullptr;
    int token_count;
    Token **tokens = tokenize(inputs, &token_count);

    for (int i = 0; i < token_count; i++)
    {
        if (i == 0)
        {
            head = tokens[i];
            continue;
        }

        head->next = tokens[i];
        head = head->next;
    }

    // 打印所有 Token 信息
    print_tokens(tokens, token_count);

    // ASTNode *ast = parse(tokens, token_count);

    // print_ast(ast);

    // free_tokens(tokens, token_count);
    // free_ast(ast);

    return 0;
}